//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cancel.asyncImage;

import android.graphics.Bitmap;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MemoryCache {
    private Map<String, Bitmap> cache = Collections.synchronizedMap(new LinkedHashMap(10, 1.5F, true));
    private long size = 0L;
    private long limit = 1000000L;

    public MemoryCache() {
        this.setLimit(Runtime.getRuntime().maxMemory() / 4L);
    }

    public void setLimit(long new_limit) {
        this.limit = new_limit;
    }

    public Bitmap get(String id) {
        try {
            return !this.cache.containsKey(id)?null:(Bitmap)this.cache.get(id);
        } catch (NullPointerException var3) {
            return null;
        }
    }

    public void put(String id, Bitmap bitmap) {
        try {
            if(this.cache.containsKey(id)) {
                this.size -= this.getSizeInBytes((Bitmap)this.cache.get(id));
            }

            this.cache.put(id, bitmap);
            this.size += this.getSizeInBytes(bitmap);
            this.checkSize();
        } catch (Throwable var4) {
            var4.printStackTrace();
        }

    }

    private void checkSize() {
        if(this.size > this.limit) {
            Iterator iter = this.cache.entrySet().iterator();

            while(iter.hasNext()) {
                Entry<String, Bitmap> entry = (Entry)iter.next();
                this.size -= this.getSizeInBytes((Bitmap)entry.getValue());
                iter.remove();
                if(this.size <= this.limit) {
                    break;
                }
            }
        }

    }

    public void clear() {
        this.cache.clear();
    }

    long getSizeInBytes(Bitmap bitmap) {
        return bitmap == null?0L:(long)(bitmap.getRowBytes() * bitmap.getHeight());
    }
}
